package com.xindz.cn.mvptest.base;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.kingja.loadsir.callback.Callback;
import com.kingja.loadsir.core.LoadService;
import com.kingja.loadsir.core.LoadSir;
import com.xindz.cn.mvptest.R;

import butterknife.ButterKnife;

/**
 * @name MvpTest
 * @class name：com.xindz.cn.mvptest.base
 * @describe describe
 * @anthor XINDZ
 * @time 2017/10/31 13:23
 * @change
 * @chang time
 * @class describe
 */
public abstract class LoadBaseActivity<V extends BaseView, P extends BasePresenter<V>> extends BaseActivity {
    private View rootView;
    protected LoadService mBaseLoadService;
    public boolean NotMVP = false;
    private P presneter;
    private V view;

    public P getPresneter() {
        return presneter;
    }

    public void setNotMVP() {
        NotMVP = true;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        rootView = View.inflate(this, R.layout.activity_base, null);
        addContent();
        setContentView(rootView);
        if (!NotMVP) initMVP();
        initView();
        initNet();
        initData();
    }

    private void initMVP() {
        if (this.presneter == null) {
            //创建P层
            this.presneter = createPresneter();
        }

        if (this.view == null) {
            //创建V层
            this.view = createView();
        }
        //判定是否为空
        if (this.presneter == null) {
            throw new NullPointerException("presneter不能够为空");
        }
        if (this.view == null) {
            throw new NullPointerException("view不能够为空");
        }
        //绑定
        this.presneter.attachView(this.view);
    }

    private void addContent() {
        FrameLayout flContent = (FrameLayout) rootView.findViewById(R.id.fl_content);
        TextView tvTitleTitle = (TextView) rootView.findViewById(R.id.tv_title_title);
        LinearLayout llTitleBack = (LinearLayout) rootView.findViewById(R.id.ll_title_back);
        tvTitleTitle.setText(getContentTitle() == null ? "" : getContentTitle());
        llTitleBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                backClick();
            }
        });
        View content = View.inflate(this, getContentView(), null);
        if (content != null) {
            FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,
                    FrameLayout.LayoutParams.MATCH_PARENT);
            flContent.addView(content, params);
            ButterKnife.bind(this, rootView);
            mBaseLoadService = LoadSir.getDefault().register(content, new Callback.OnReloadListener() {
                @Override
                public void onReload(View v) {
                    onNetReload(v);
                }
            });
        }
    }

    private void backClick() {
        finish();
    }

    protected abstract String getContentTitle();

    protected abstract int getContentView();

    protected abstract void initView();

    protected abstract void initData();

    protected abstract void initNet();

    protected abstract void onNetReload(View v);

    //并不知道具体的P是哪一个实现类，由他的子类决定(BaseActivity子类决定具体类型)
    public abstract P createPresneter();

    //并不知道具体的V是哪一个实现类，由他的子类决定(BaseActivity子类决定具体类型)
    public abstract V createView();
}
